﻿using System;

namespace Exercises2.Model.math {
    /// <summary>
    /// Complex number 
    /// </summary>
    public class Complex {
        public double Real { get; set; }
        public double Imaginary { get; set; }


        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="real"></param>
        /// <param name="imaginary"></param>
        public Complex(double real, double imaginary) {
            Console.WriteLine("Complex init...");

            Real = real;
            Imaginary = imaginary;
        }


        /// <summary>
        /// adding two complex numbers
        /// </summary>
        /// <param name="cN2"></param>
        /// <returns></returns>
        /// 
        public Complex Add(Complex cN2) {
            double tempReal = (Real + cN2.Real);
            double tempImag = (Imaginary + cN2.Imaginary);
            Complex temp = new Complex(tempReal, tempImag);

            return temp;
        }


        /// <summary>
        /// Subtracting two complex numbers
        /// </summary>
        /// <param name="cN2"></param>
        /// <returns></returns>
        public Complex Subtract(Complex cN2) {
            double tempReal = Real - cN2.Real;
            double tempImag = Imaginary - cN2.Imaginary;
            Complex temp = new Complex(tempReal, tempImag);
            return temp;
        }


        /// <summary>
        /// Multiply two complex numbers
        /// </summary>
        /// <param name="cN2"></param>
        /// <returns></returns>
        public Complex Multiply(Complex cN2) {
            double tempReal = (Real*cN2.Real) - (Imaginary*cN2.Imaginary);
            double tempImag = (Real*cN2.Imaginary) + (cN2.Real*Imaginary);
            Complex temp = new Complex(tempReal, tempImag);
            return temp;
        }


        /// <summary>
        /// Divide two complex numbers
        /// </summary>
        /// <param name="cN2"></param>
        /// <returns></returns>
        public Complex Divide(Complex cN2) {
            double tempReal = (((Real*cN2.Real) +
                                (Imaginary*cN2.Imaginary))/((Math.Pow(cN2.Real, 2) + Math.Pow(cN2.Imaginary, 2))));
            double tempImag = (((cN2.Real*Imaginary) - (Real*cN2.Imaginary))/
                               (Math.Pow(cN2.Real, 2) + Math.Pow(cN2.Imaginary, 2)));
            Complex temp = new Complex(tempReal, tempImag);
            return temp;
        }

        /// <summary>
        /// Conjugate of a complex number
        /// </summary>
        /// <returns></returns>
        public Complex Conj() {
            double tempImag = Imaginary*(-1);
            Complex temp = new Complex(Real, tempImag);
            return temp;
        }


        /// <summary>
        /// Displaying the complex number
        /// </summary>
        /// <returns></returns>
        public override string ToString() {
            string temp;
            if (Imaginary >= 0) {
                temp = string.Format("{0:F2}+{1:F2}j ", Real, Imaginary);
            } else {
                temp = string.Format("{0:F2}{1:F2}j ", Real, Imaginary);
            }
            return temp;
        }


        public bool Equals(Complex cn) {
            bool temp;
            if (Real == cn.Real && Imaginary == cn.Imaginary) {
                temp = true;
            } else {
                temp = false;
            }
            return temp;
        }

        public override bool Equals(object obj) {
            if (ReferenceEquals(null, obj)) {
                return false;
            }
            if (ReferenceEquals(this, obj)) {
                return true;
            }
            if (obj.GetType() != this.GetType()) {
                return false;
            }
            return Equals((Complex) obj);
        }
    }
}